package com.temp.local_date;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.Locale;

public class TestDateMain {
    public static void main(String[] args) {
        //格式化
        DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate now = LocalDate.of(2024, 10, 8);
        LocalDate now2 = now.plusMonths(1);
        LocalDate now3 = now.plusMonths(-12);
        System.out.println(now.getMonth());
        System.out.println(now.getDayOfWeek());
        System.out.println(now2.format(formatterDate));
        System.out.println(now3.format(formatterDate));
        LocalDate today = LocalDate.now();
        //本月的第一天
        LocalDate firstday = LocalDate.of(today.getYear(), today.getMonth(), 1);
        System.out.println(firstday.format(formatterDate));
        //本月的最后一天
        LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(lastDay.format(formatterDate));

        // 获取默认的星期几为一周的第一天（通常是周一）和一周的第一天（通常是周
        System.out.println("Week number of the year: " + weekOfYear( DateUtil.parseDate("2025-10-29")));
        System.out.println("Week number of the year: " + weekOfYear( DateUtil.parseDate("2025-10-08")));
    }

    /**
     * 取得第几周，Hutool默认的可能有问题，没有设置FirstDayOfWeek
     * @param date
     * @return
     */
    public static String weekOfYear(Date date) {
        int seq = DateTime.of(date).setFirstDayOfWeek(Week.MONDAY).weekOfYear();
        return DateUtil.year(date)+"_" + seq;
    }
}
